/**
 * 菜单操作
 */
$(function(){

    //检查当前用户是否已经是登录状态
	/*$.ajax({
	 url:'http://devsso.zcjb.com.cn/cas/checkLogin',
	 type:"GET",
	 dataType:'json',
	 data:{"service":"http://192.168.100.8:28180/scfw/userController/login.html"},
	 xhrFields: {
	 withCredentials: true
	 },
	 success:function(data){
	 //console.log(JSON.stringify(data));
	 if(data.code=='1') {
	 data.username=data.loginName;
	 $.ajax({
	 url: root+'/userController/ssoLogin.html',
	 dataType: 'json',
	 type: "POST",
	 data: data,
	 success: function (r) {
	 //console.log(JSON.stringify(r));
	 if(r.isSuccess == 1){
	 location.href=root+"foodController/queryFoods.html";
	 }
	 },
	 error: function (r) {
	 //console.log(JSON.stringify(r));
	 }
	 });
	 }else{
	 //判断是否需要显示验证码
	 if(data.captchaShow){
	 //以下是显示验证码的逻辑
	 /!*$(".captchaDiv").show();
	 $(".regist").hide();
	 $("#captchaImg").attr("src","http://localpass.zcjb.com.cn:8080/cas/captcha?r="+Math.floor(Math.random() * 10000) );
	 $("#loginFailTimesInput").val(3);*!/
	 }
	 }
	 },
	 error:function(data){
	 //console.log(JSON.stringify(data));
	 }
	 });*/

    $.getJSON('http://devsso.zcjb.com.cn/cas/checkLogin?service=http://192.168.100.8:28180/scfw/userController/login.html&jsonpCallback=?', function(data){
        //console.log(JSON.stringify(data));
        if(data.code=='1') {
            data.username=data.loginName;
            $.ajax({
                url: root+'/userController/ssoLogin.html',
                dataType: 'json',
                type: "POST",
                data: data,
                success: function (r) {
                    //console.log(JSON.stringify(r));
                    if(r.isSuccess == 1){
                        location.href=root+"foodController/queryFoods.html";
                    }
                },
                error: function (r) {
                    //console.log(JSON.stringify(r));
                }
            });
        }else{
            //判断是否需要显示验证码
            if(data.captchaShow){
                //以下是显示验证码的逻辑
            }
        }
    });


    var isLogin = $("#isLogin").val();
    //alert(isLogin);
    if('yes'==isLogin){
        location.href=root+"foodController/queryFoods.html";
    }
	/*$(".loginDiv").show();*/
    $(".loginDiv").animate({marginTop:'250px'},200,function(){
        $(".loginDiv").animate({marginTop:'200px'},100);
    });


    $("input[name='username'],input[name='password']").bind('keypress',function(event){
        if(event.keyCode == "13"){
            console.info("回车了");
            $(".loginBtn").trigger('click');
        }
    });

    $(".loginBtn").bind("click",function(){
        var flag = loginValide();
        if(flag=="false"){return;}
        $("#userLogin").ajaxSubmit({
            dataType : 'json',
            contentType : 'text/plain',
            xhrFields: {
                withCredentials: true
            },
            success : function(data) {
                if (data.code == 1) {

                    /*$.getJSON("https://devsso.zcjb.com.cn/cas/setSSOToken?ssoToken=" + data.sso_token + "&jsonpCallback=?",
                        function(r){
                            alert("code:"+r.code);
                            alert("ssoToken:"+r.ssoToken);
                        }
                    );*/

                    $.ajax({
                        type: "GET",
                        async: false,
                        url: "https://devsso.zcjb.com.cn/cas/setSSOToken?ssoToken=" + data.sso_token,
                        dataType: "jsonp",
                        cache: false,
                        jsonp: "jsonpCallback",
                        success: function (r) {
                            //返回的数据用data.d获取内容
                            //alert("code:"+r);
                        },
                        error: function (err) {
                            //alert(err);
                        }
                    });

                    //console.log(JSON.stringify(data));
                    data.username=data.loginName;
                    $.ajax({
                        url:root+'/userController/ssoLogin.html',
                        dataType:'json',
                        type:"POST",
                        data:data,
                        success:function(r){
                            //console.log(JSON.stringify(r));
                            window.location.href=root+"foodController/queryFoods.html";
                        },
                        error:function(r){
                            //console.log(JSON.stringify(r));
                        },complete:function(a,b){

                        }
                    });
                }else{
                    alt(data.message);
                }
            }
        });
    });
    //登录验证
    function loginValide(){
        var uname = $.trim($("input[name='username']").val());
        var upwd = $.trim($("input[name='password']").val());
        if(uname==''){
            alt('请输入用户名！',function(r){
            });
            return "false";
        }else if(upwd==''){
            alt('请输入密码！');
            return "false";
        }else{
            return "true";
        }
    }
    //注册
    $(".submitBtn").bind('click',function(){
        var flag = registValide();
        if(flag=="false"){return;}
        $("#userRegist").ajaxSubmit({
            dataType : 'json',
            success : function(data) {
                if (data.isSuccess == 1) {
                    alt(data.msgInfo,function(){
                        window.location.href=root+"userController/login.html";
                    });
                }else{
                    alt(data.msgInfo);
                }
            }
        });
    });
    //注册验证
    function registValide(){
        var uname = $.trim($("input[name='userName']").val());
        var upwd = $.trim($("input[name='userPwd']").val());
        var upwd2 = $.trim($("input[name='userPwd2']").val());
        var trueName = $.trim($("input[name='nickName']").val());
        if(uname==''){
            alt('请输入用户名！');
            return "false";
        }else if(uname.length<4){
            alt('用户名长度至少为4！');
            return "false";
        }else if(upwd==''){
            alt('请输入密码！');
            return "false";
        }else if(upwd!=upwd2){
            alt('您两次输入的密码不一致！');
            return "false";
        }else if(trueName==''){
            alt('请输入您的真实姓名！');
            return "false";
        }else{
            return "true";
        }
    }
    //返回登录页面
    $(".backBtn").bind('click',function(){
        window.location.href=root+"userController/login.html";
    });
});